home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Standard question - pointer initialization
- Date: Wed, 06 Mar 96 19:54:33 GMT
- Organization: none
- Message-ID: <826142073snz@genesis.demon.co.uk>
- References: <4hk9un$906@hammer.msfc.nasa.gov>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4hk9un$906@hammer.msfc.nasa.gov>
- Brian.Day@msfc.nasa.gov "Brian Day" writes:
-
- >Hi all,
- >
- >I don't have a copy of the ANSI standard handy, and was wondering
- >if y'all could answer a question for me.
- >
- >We were recently handed a coding standard which said:
- >
- > "Always initialize pointers when they are declared"
- >
- >Doesn't the standard say something about some pointers
- >being automatically initialized to NULL, or something like that?
-
- Pointers are just like any other type of variable in this respect: static
- duration variables (i.e. defined as static or outside functions) are
- always initialised as program startup. They are initialised as if set to
- the value zero if there is no explicit initialisation in the source code.
- In the case of pointers this means they are initialised to null pointers
- of the respective type.
-
- >Is this coding standard wise?
-
- It is mostly if not entirely pointless. There is no point in initialising
- a variable if there is no sensible value to initialise it to. You might
- initialise pointers to the null pointer but the code might blow up anyway
- when encountering a null pointer. A much better approach is to use a
- compiler or failing that a lint utility which can detect and diagnose where
- code may be accessing uninitialised variables. This is a fairly routine
- dataflow analysis for a compiler although you may have to use a higher
- level of optimisation to get it.
-
- It is more useful to initialise only those variables that need it,
- then when reading the code you can see where the initialisation is
- important i.e. you get more readable and maintainable code.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-